package dyyx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommUtil {

    public static final String SEP = ",";
    public static final String EMPTY = "";

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    public static List<String> split(String str, String sep) {
        return split(str, sep, false);
    }

    public static List<String> split(String str, String sep, boolean ignoreEmpty) {
        if (str == null) {
            return null;
        }
        if (sep == null || EMPTY.equals(sep)) {
            sep = SEP;
        }

        List<String> list = new ArrayList<String>();
        int fromIndex = 0;

        int sepLen = sep.length();
        int pos = 0;
        String tmp = null;
        // a,b,c
        while (true) {
            pos = str.indexOf(sep, fromIndex);
            if (pos < 0) {
                list.add(str.substring(fromIndex));
                break;
            }
            tmp = str.substring(fromIndex, pos);
            if (ignoreEmpty && tmp.length() <= 0) {
                // ignore
            } else {
                list.add(tmp);
            }
            fromIndex = fromIndex + tmp.length() + sepLen;
        }
        return list;
    }

    public static Map<String, String> parseHttpRequestParams(String url) {
        Map<String, String> map = new HashMap<>();
        if (isBlank(url)) {
            return map;
        }
        int index = url.indexOf("?");
        if (index < 0) {
            return map;
        }
        String str = url.substring(index + 1);
        List<String> kvs = split(str, "&");

        if (kvs == null || kvs.isEmpty()) {
            return map;
        }
        for (String kv : kvs) {
            if (isBlank(kv)) {
                continue;
            }
            int eqindex = kv.indexOf("=");
            if (eqindex <= 0) {
                continue;
            }
            String key = kv.substring(0, eqindex);
            if (isBlank(key)) {
                continue;
            }
            String value = kv.substring(eqindex + 1);
            if (isBlank(value)) {
                continue;
            }
            map.put(key, value);
        }
        return map;
    }

    public static int getInt(String str) {
        if (isBlank(str)) {
            return 0;
        }
        try {
            return Integer.parseInt(str);
        } catch (Throwable e) {
            return 0;
        }
    }

    public static int getInt(Map<String, String> map, String name) {
        if (map == null || map.isEmpty()) {
            return 0;
        }
        return getInt(map.get(name));
    }

    public static void main(String[] args) throws Exception {

    }
}
